package com.leetcode.根据数据结构分类.数组;

/**
 * @author: xiaomi
 * @date: 2021/2/22
 * @description: 766. 托普利茨矩阵
 * https://leetcode-cn.com/problems/toeplitz-matrix/
 */
public class A_766_托普利茨矩阵 {

	public static void main(String[] args) {
		A_766_托普利茨矩阵 action = new A_766_托普利茨矩阵();
		test1(action);
	}

	public static void test1(A_766_托普利茨矩阵 action) {
		// true
		int[][] matrix = {{1,2,3,4},{5,1,2,3},{9,5,1,2}};
		boolean res = action.isToeplitzMatrixFirst(matrix);
		System.out.println("res = " + res);
	}

	/**
	 * 考虑用一个动态数组的形式
	 * 那么这个就是进阶一的解法！
	 * @param matrix
	 * @return
	 */
	public boolean isToeplitzMatrixFirst(int[][] matrix) {
		//m 行
		int m = matrix.length;
		//n 列
		int n = matrix[0].length;
		int[] compareArr = new int[n];
		//1.加载第一行到动态数组
		for (int i = 0; i < n; i++) {
			compareArr[i] = matrix[0][i];
		}
		int beginIndex = 0;
		int endIndex = n - 1;
		for (int i = 1; i < m; i++) {
			for (int j = 1; j < n; j++) {

				if (matrix[i][j] != compareArr[beginIndex]) {
					return false;
				}
				if (beginIndex == endIndex) {
					beginIndex = 0;
				} else {
					beginIndex++;
				}
			}
			compareArr[beginIndex] = matrix[i][0];
		}
		return true;
	}
}
